<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSerialStatus extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $goods_id = $this->request->post('goods_id');
            if(!empty($goods_id)){
                $where['goods_id'] = ['in',$goods_id];
            }
            $serial = $this->request->post('serial');
            if(!empty($serial)){
                $where['serial'] = ['in',$serial];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_serial_log')
                    ->where($where)
                    ->page($page,$limit)
                    ->field('max(id) as id')
                    ->group('serial')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_serial_log')
                    ->where($where)
                    ->field('max(id) as id')
                    ->group('serial')
                    ->select();
            }
            $row_count =Db::name('kuyou_goods_serial_log')
                ->where($where)
                ->field('max(id) as id')
                ->group('serial')->count();

            foreach ($list as $list_k=>$list_v){
                $info = Db::name('kuyou_goods_serial_log')->where(['id'=>$list_v['id']])->find();
                $list[$list_k]['serial']        = $info['serial'];
                $list[$list_k]['business_type'] = $info['business_type'];
                $list[$list_k]['store_status']  = $info['store_status'];
                $list[$list_k]['purchase_time'] = $info['purchase_time'];
                $list[$list_k]['createtime']    = empty($info['createtime'])?'':date('Y-m-d',$info['createtime']);
                $list[$list_k]['purchase_code'] = $info['purchase_code'];
                $list[$list_k]['remarks']       = $info['remarks'];

                $goods = Db::name('kuyou_goods')->where(['id'=>$info['goods_id']])->field('goods_code,goods_name,goods_barcode,goods_specification_id')->find();
                $list[$list_k]['goods_code'] = $goods['goods_code'];
                $list[$list_k]['goods_name'] = $goods['goods_name'];
                $list[$list_k]['goods_spec'] = $goods['goods_specification_id'];

                $stores = Db::name('kuyou_stores')->where(['id'=>$info['stores_id']])->field('store_code,name')->find();
                $list[$list_k]['store_name'] = $stores['name'];
                //供应商/客户
                if($info['business_type']==2 || $info['business_type']==3 || $info['business_type']==7 || $info['business_type']==8){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_suppliers')->where(['id'=>$info['customer_id']])->value('supplyer_name');
                }else if($info['business_type']==4 || $info['business_type']==5 || $info['business_type']==9 || $info['business_type']==10){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_customers')->where(['id'=>$info['customer_id']])->value('customer_name');
                }
            }
            $result = array("count" => $row_count, "list" => $list);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
